package pl.gpms.server.persistance.map;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import pl.gpms.client.dto.SymptomDTO;
import pl.gpms.server.persistance.core.XMLObject;
import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "T_SYMPTOM")
public class Symptom implements Serializable, XMLObject {

	private static final long serialVersionUID = 2569133686270539403L;
	private Long id;
	private String symptomName;
	private Double symptomConfidence;
	private Estimate estimate;
	
	public Symptom() {}
	public Symptom( Estimate estimate, String symptomName, Double symptomConfidence ) {
		this.estimate = estimate;
		this.symptomName = symptomName;
		this.symptomConfidence = symptomConfidence;
	}
	public Symptom( SymptomDTO symptom ) {
		this.symptomName = symptom.getSymptomName();
		this.symptomConfidence = symptom.getSymptomConfidence();
		this.estimate = new Estimate( symptom.getEstimate() );
		this.id = symptom.getId();
	}
	
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column( name="SMM_ID", unique = true, nullable = false )
	public Long getId() {
		return this.id;
	}
	
	public void setId( Long id ) {
		this.id = id;
	}
	
	
	@Column( name="SMM_NAME", unique = false, nullable = false, length = 45 )
	public String getSymptomName() {
		return symptomName;
	}
	
	public void setSymptomName( String symptomName ) {
		this.symptomName = symptomName;
	}
	
	@Column( name="SMM_CONFIDENCE", unique = false, nullable = false  )
	public Double getSymptomConfidence() {
		return this.symptomConfidence;
	}
	
	public void setSymptomConfidence( Double symptomConfidence ) {
		this.symptomConfidence = symptomConfidence;
	}
	
	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn( name="id", nullable = false, updatable = false, insertable = false )
	public Estimate getEstimate() {
		return estimate;
	}
	
	public void setEstimate( Estimate estimate ) {
		this.estimate = estimate;
	}
	
	public String toString() {
		return new String( symptomName + ": " + symptomConfidence );
	}
	
	public String toXMLString() {
		
		String result = new String();
		result = "<symptom>";
		result += "<id>" + this.id + "</id>";
		result += "<symptomName>" + this.symptomName + "</symptomName>";
		result += "<symptomConfidence>" + this.symptomConfidence + "</symptomConfidence>";
		result += "</symptom>";
		return result;
	}
}
